package com.xuanmiao.smsback.web.controller.operator;

import com.panshi.hujin2.base.common.factory.MessageFactory;
import com.panshi.hujin2.base.domain.result.BasicResultCode;
import com.xuanmiao.smsback.dao.mapper.sms.MsgSendManageMapper;
import com.xuanmiao.smsback.domain.qo.AuditRecordInfoQO;
import com.xuanmiao.smsback.domain.qo.ListExpenseCalendarInfoQO;
import com.xuanmiao.smsback.domain.qo.RechargeRecordInfoQO;
import com.xuanmiao.smsback.domain.qo.UserListInfoQO;
import com.panshi.hujin2.base.common.util.DozerUtils;
import com.panshi.hujin2.base.domain.result.BasicResult;
import com.xuanmiao.smsback.web.controller.vo.AuditRecordInfoVO;
import com.xuanmiao.smsback.web.controller.vo.ExpenseCalendarInfoVO;
import com.xuanmiao.smsback.web.controller.vo.RechargeRecordInfoVO;
import com.xuanmiao.smsback.web.controller.vo.UserListInfoVO;

import com.xuanmiao.smsback.model.AuditRecordInfoBO;
import com.xuanmiao.smsback.model.ExpenseCalendarInfoBO;
import com.xuanmiao.smsback.model.RechargeRecordInfoBO;
import com.xuanmiao.smsback.model.UserListInfoBO;
import com.xuanmiao.smsback.service.platform.operator.UserListInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Collections;
import java.util.List;

@Api("商户管理")
@RequestMapping(value = "/userListInfo")
@RestController
public class UserListInfoController {
    private final Logger LOGGER = LoggerFactory.getLogger(getClass());
    @Autowired
    private UserListInfoService userListInfoService;
    @Autowired
    private MsgSendManageMapper mgSendManageMapper;

    @ApiOperation("商户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "createTimeStart" ,value = "创建时间开始" ,dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd" ,value = "创建时间结束" ,dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "status" ,value = "账户状态0--提交注册 1--注册被拒绝 2--激活 3--停用" ,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "judge" ,value = "余额判断 大小等于" ,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "balance" ,value = "余额" ,dataType = "double",paramType = "query"),
            @ApiImplicitParam(name = "businessArea" ,value = "营运区域 0--印尼" ,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "accountNo" ,value = "账户" ,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "companyName" ,value = "公司名称" ,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "businessNo" ,value = "商户号" ,dataType = "String",paramType = "query"),
    })
    @GetMapping()
    BasicResult<List<UserListInfoVO>> all(@ApiIgnore UserListInfoQO userListInfoQO){

        try {
            List<UserListInfoBO> listBOs= userListInfoService.getList(userListInfoQO);
            List<UserListInfoVO> listVOs = Collections.emptyList();
            if (listBOs.size() > 0) {
                listVOs = DozerUtils.convertList(listBOs, UserListInfoVO.class);
                listVOs.stream().forEach(listVO -> {
                    listVO.setCreateTimeStr(DateFormatUtils.format(listVO.getCreateTime(), "yyyy/MM/dd HH:mm"));
                });
            }

            return BasicResult.ok(listVOs,userListInfoQO.getPage());
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);

            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
    @ApiOperation("主键查询商户详情")
    @GetMapping("{id:\\d+}")
    BasicResult<UserListInfoVO> queryPrimaryKey(@PathVariable Integer id) {
        try {
            UserListInfoBO res = userListInfoService.getById(id);

            return BasicResult.ok(DozerUtils.convert(res, UserListInfoVO.class));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
    @ApiOperation("主键查询充值记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,value = "id" ,dataType = "int",paramType = "query"),

    })
    @GetMapping("/recharge")
    BasicResult<List<RechargeRecordInfoVO>> queryCharge(@ApiIgnore RechargeRecordInfoQO rechargeRecordInfoQO) {
        try {
            List<RechargeRecordInfoBO>  listBOs = userListInfoService.getRechargeById(rechargeRecordInfoQO);
            List<RechargeRecordInfoVO> listVOs = Collections.emptyList();
            if (listBOs.size() > 0) {
                listVOs = DozerUtils.convertList(listBOs, RechargeRecordInfoVO.class);
            }
            listVOs.stream().forEach(listVO -> {
                if (listVO.getEndTime() != null) {
                    listVO.setEndTimeStr(DateFormatUtils.format(listVO.getEndTime(), "yyyy/MM/dd HH:mm"));
                }
                listVO.setCreateTimeStr(DateFormatUtils.format(listVO.getCreateTime(), "yyyy/MM/dd HH:mm"));
                if (listVO.getModifyTime()!= null) {
                    listVO.setModifyTimeStr(DateFormatUtils.format(listVO.getModifyTime(), "yyyy/MM/dd HH:mm"));
                }
            });
            return BasicResult.ok(listVOs, rechargeRecordInfoQO.getPage());

        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);

            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
    @ApiOperation("主键查询消费记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "createTimeStart" ,value = "创建时间开始" ,dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd" ,value = "创建时间结束" ,dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "expenseStatus" ,value = "消费状态0--完成 1--取消 2--暂停失效 " ,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "queueId" ,value = "队列ID " ,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "id" ,value = "商户id" ,dataType = "int",paramType = "query"),

    })
            @GetMapping("/expense")
    BasicResult<List<ExpenseCalendarInfoVO>> queryExpense(@ApiIgnore ListExpenseCalendarInfoQO listExpenseCalendarInfoQO) {
        try {
            List<ExpenseCalendarInfoBO> listBOs = userListInfoService.getExpenseById(listExpenseCalendarInfoQO);
            List<ExpenseCalendarInfoVO> listVOs = Collections.emptyList();
            if (listBOs.size() > 0) {
                listVOs = DozerUtils.convertList(listBOs, ExpenseCalendarInfoVO.class);
            }
            listVOs.stream().forEach(listVO -> {
                listVO.setConsumerCode(mgSendManageMapper.selectByPrimaryKey(listVO.getQueueId()).getConsumerCode());

                if (listVO.getEndTime() != null) {
                    listVO.setEndTimeStr(DateFormatUtils.format(listVO.getEndTime(), "yyyy/MM/dd HH:mm"));
                }
                if (listVO.getSendTime() != null) {
                    listVO.setSendTimeStr(DateFormatUtils.format(listVO.getSendTime(), "yyyy/MM/dd HH:mm"));
                }
                listVO.setCreateTimeStr(DateFormatUtils.format(listVO.getCreateTime(), "yyyy/MM/dd HH:mm"));

            });
            return BasicResult.ok(listVOs, listExpenseCalendarInfoQO.getPage());
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);

            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
    @ApiOperation("主键查询审核记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,value = "id" ,dataType = "int",paramType = "query"),
    })
    @GetMapping("/audit")
    BasicResult<List<AuditRecordInfoVO>> queryAudit(@ApiIgnore AuditRecordInfoQO auditRecordInfoQO) {
        try {
            List<AuditRecordInfoBO> listBOs = userListInfoService.getAuditById(auditRecordInfoQO);
            List<AuditRecordInfoVO> listVOs = Collections.emptyList();
            if (listBOs.size() > 0) {
                listVOs = DozerUtils.convertList(listBOs, AuditRecordInfoVO.class);
            }
            return BasicResult.ok(listVOs, auditRecordInfoQO.getPage());
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);

            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
}
